<?php 
class App extends Module {
    private $router = array();
    private $site = array();
    private $base_url = '';
    
    public function run() {
        $router = $this->get_router();
        $m = $router['m'];
        
        $module = m($m);
        $method = tos_http_get('method');
        
        $method = empty($method) ? 'act' : $method;
        $a = $method . '_' . $router['a'];
        
        if ( ! method_exists($module, $a)) {
            echo $m . ' - ' . $a;
            tos_die('error request!');
        }
        
        $module->$a();
        if ($method === 'act') {
            $out = out();
            $site = $this->get_site();
            $out->add_title($site['caption']);
            $out->add_block(new View('debug'), 'debug');
            $out->display('html');
        }
    
        
    }
    
    
    public function get_router() {
        if ( ! $this->router) {
            $site = $this->get_site();
            $site_level = $site['level'];
            
            $m = $site['router']['m'];
            $a = $site['router']['a'];
                
            if ($site_level !== SITE_LEVEL_LIMIT) {
                $request_m = tos_http_get('m');
                $request_a = tos_http_get('a');
                
                if ( ! empty($request_m) && ! empty($request_a)) {
                    $m = $request_m;
                    $a = $request_a;
                }
            }
            
            $router_model = $this->model('router');
            $router_info = $router_model->get_info($m, $a);
            $access_mode = ($router_info && isset($router_info['access_mode'])) ?
                $router_info['access_mode'] : ROUTER_ACCESS_PRIVATE;
            
            if ($access_mode !== ROUTER_ACCESS_PUBLIC) {
                $user_module = m('user');
                if (true !== $user_module->logined()) {
                    $m = 'user';
                    $a = 'login';
                } else if ( ! $user_module->is_admin()) {
                    if ($access_mode === ROUTER_ACCESS_POSITION) {
                        $router_id = $router_info['id'];
                        $position_ids = $user_module->get_position_ids();
                        if ( ! $router_model->has_position_ids($router_id, $position_ids)) {
                            $m = 'app';
                            $a = 'no_permission';
                        }
                    }
                }
            }
            $this->router = array(
                'id'    => $router_info['id'],
                'm'     => $m,
                'a'     => $a
            );
        }
        return $this->router;
    }
    
    
    
    public function get_site() {
        if ( ! $this->site) {
            $server_name = $_SERVER['SERVER_NAME'];
            $dir = ROOT_DIR . '/site/';
            $file = $dir . $server_name . '.setting.php';
            if ( ! file_exists($file)) {
                $file = $dir . 'default.setting.php';
            }
            $setting = include $file;
            $site = array(
                'caption'   => $setting['caption'],
                'level'     => $setting['level'],
                'router'    => $setting['router'],
                'server_name'   => $server_name,
                'base_path'     => isset($setting['base_path']) ? $setting['base_path'] : ''
            );
            
            if (isset($setting['cdn'])) {
                m('cdn')->set_opts($setting['cdn']);
            }
            $out = out();
            if (isset($setting['css'])) {
                $csses = $setting['css'];
                foreach ($csses as $css) {
                    $out->add_css(tos_cdn_url($css));
                }
            }
            if (isset($setting['js'])) {
                $jses = $setting['js'];
                foreach ($jses as $js) {
                    $out->add_prejs(tos_cdn_url($js));
                }
            }
            if (isset($setting['block'])) {
                $boxes = $setting['block'];
                foreach ($boxes as $box_name => $box) {
                    foreach ($box as $block) {
                        $out->add_block(new View($block), $box_name);
                    }
                }
            }
            if (isset($setting['node_type'])) {
                m('node')->add_types($setting['node_type']);
            }
            if (isset($setting['theme'])) {
                tos_set_config('theme', $setting['theme']);
            }
            if (isset($setting['databases'])) {
                $databases = $setting['databases'];
                foreach ($databases as $keyword => $database) {
                    tos_add_db($keyword, $database);
                }
            }
            if (isset($setting['debug_mode']) && $setting['debug_mode'] == true) {
                tos_debug_mode(true);
            }
            $this->site = $site;
        }
        return $this->site;
    }
    
    
    
    public function get_base_url() {
        if ( ! $this->base_url) {
            $site = $this->get_site();
            $server_name = $site['server_name'];
            $base_path = trim($site['base_path']);
            //$base_path = empty($base_path) 
            
            $base_url = $server_name . '/' . $base_path;
            /*if ( ! empty($base_apth)) {
                $base_url .= '/' . $base_path;
            } */
            $this->base_url = $base_url;
        }
        return $this->base_url;
    }
    
    
    public function get_tpl_files($relative_dir) {
        $full_dir = ROOT_DIR . '/theme/' . tos_get_config('theme') . '/' . $relative_dir;
        if ( ! is_dir($full_dir)) {
            tos_dir("Cannot find tpl dir $relative_dir.");
        }
        
        $tpl_names = array();
        $d = dir($full_dir);
        
        while (false !== ($entry = $d->read())) {
            if ($entry[0] === '.' || $entry[0] === '~') {
                continue;
            }
            $dot_pos = strpos($entry, '.');
            if ($dot_pos) {
                $tpl_names[] = substr($entry, 0, $dot_pos);
            }
        }
        $d->close();
        
        return $tpl_names;
    }
}

?>